import router from "./router";

import { AxiosCanceler } from "../api/helper/axiosCancel";

const axiosCanceler = new AxiosCanceler();
/**
 * 使用递归处理路由菜单
 * @param newArr 所有菜单数组
 */
export function handleRouter(routerList: Menu.MenuOptions[], newArr: string[] = []) {
	routerList.forEach((item: Menu.MenuOptions) => {
		typeof item === "object" && item.path && newArr.push(item.path);
		item.children && item.children.length !== 0 && handleRouter(item.children, newArr);
	});
	return newArr;
}

// * 路由拦截
// 记录路由
router.beforeEach(async (to, from, next) => {
	// * 在跳转路由之前，清除所有的请求
	axiosCanceler.removeAllPending();
	
  let title:any = to.name;
  //如果设置标题就显示，没设置使用默认
  if (null != to.meta && null != to.meta.title && '' !== to.meta.title) {
      title = to.meta.title;
  }
  //非生产环境添加标识
  if (/ip(hone|od|ad)/i.test(navigator.userAgent)) {
      setTimeout(function () {
          document.title = title;
          let _iframe = document.createElement('iframe');
          _iframe.style.display = 'none';
          _iframe.onload = function () {
              setTimeout(function () {
                  document.body.removeChild(_iframe);
              }, 0);
          };
          document.body.appendChild(_iframe);
      }, 0);
  } else {
      document.title = title;
  }
  //正常放行
  next();
});


export default router;
